package com.example.goodsmanagementcabinet.otto;

import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;

public class OttoManager extends Bus {
    public static OttoManager sOttoManager;


    private OttoManager() {
        super(ThreadEnforcer.ANY);
    }

    public static OttoManager getInstance() {
        if (sOttoManager == null) {
//            synchronized (OttoManager.class) {
                sOttoManager = new OttoManager();
//            }
        }
        return sOttoManager;
    }

    /**
     * 订阅
     *
     * @param subscriber
     */
    public void onSubscribe(Object subscriber) {
        register(subscriber);
    }

    /**
     * 解除订阅
     *
     * @param subscriber
     */
    public void offSubscribe(Object subscriber) {
        unregister(subscriber);
    }

    /**
     * 发送事件
     *
     * @param msg
     */
    public void deliverMsg(OttoMsg msg) {
        post(msg);
    }

    public void deliverMsg(String which, Object data) {
        OttoMsg msg = new OttoMsg(which, data);
        deliverMsg(msg);
    }
}
